package com.test.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DataSourceConfig {

    @Bean("dataSource")
    @ConfigurationProperties(prefix = "dbcommon.master")
    public DataSource dbcommonMaster() {
        return DataSourceBuilder.create().type(ComboPooledDataSource.class).build();
    }

//    @Bean("dbcommonSlave")
//    @ConfigurationProperties(prefix = "dbcommon.slave")
//    public DataSource dbcommonSlave() {
//        return DataSourceBuilder.create().type(ComboPooledDataSource.class).build();
//    }

//    @Bean("dbcommonMasterSlaveDataSource")
//    public DataSource dbcommonMasterSlaveDataSource(DataSource dbcommonMaster,DataSource dbcommonSlave) {
//        MasterSlaveDataSource masterSlaveDataSource = new MasterSlaveDataSource();
//        Map<Object, Object> targetDataSources = new HashMap<>();
//        targetDataSources.put(Type.Master.value(), dbcommonMaster);
//        targetDataSources.put(Type.Slave.value(), dbcommonSlave);
//        masterSlaveDataSource.setTargetDataSources(targetDataSources);
//        masterSlaveDataSource.setDefaultTargetDataSource(dbcommonMaster);
//        return masterSlaveDataSource;
//    }
//
//
//    @Bean("dataSource")
//    public DataSource dynamicDataSource(DataSource dbcommonMasterSlaveDataSource) {
//        DynamicDataSource dynamicDataSource = new DynamicDataSource();
//        Map<Object, Object> targetDataSources = new HashMap<>();
//        targetDataSources.put(DBName.COMMON.value(), dbcommonMasterSlaveDataSource);
//        //targetDataSources.put(DBName.ROUTING.value(), routingDataSrouce());
//        dynamicDataSource.setTargetDataSources(targetDataSources);
//        dynamicDataSource.setDefaultTargetDataSource(dbcommonMasterSlaveDataSource);
//        return dynamicDataSource;
//    }
}
